<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;

use App\Request\User\Auth\CodeRequest;
use App\Request\User\Auth\LoginRequest;
use App\Request\User\Auth\registerRequest;
use App\Request\User\Auth\retrieveRequest;
use App\Request\User\Auth\retrievePhoneRequest;
use App\Service\SMSService;
use App\Service\User\UserService;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Snowflake\IdGeneratorInterface;

/**
 * 用户鉴权控制器
 *
 * @AutoController()
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Controller\admin
 */
class AuthController extends Controller
{
    /**
     * 用户服务
     *
     * @Inject
     * @var UserService
     */
    private $service;

    /**
     * 短信服务
     *
     * @Inject()
     * @var SMSService
     */
    private $sms;

    /**
     * @Inject()
     * @var IdGeneratorInterface
     */
    private $generator;

    /**
     * 登录接口
     *
     * @param registerRequest $request
     */
    public function login(registerRequest $request)
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);
        isset($params['login_pwd']) && $params['login_pwd'] = trim($params['login_pwd']);

        $result = $this->service->login($params['phone'], $params['login_pwd']);

        $this->success($result);
    }

    /**
     * 注册接口
     *
     * @param LoginRequest $request
     */
    public function register(LoginRequest $request)
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);
        isset($params['login_pwd']) && $params['login_pwd'] = trim($params['login_pwd']);
        isset($params['code']) && $params['code'] = trim($params['code']);
        isset($params['invitation_code']) && $params['invitation_code'] = trim($params['invitation_code']);

        $result = $this->service->register($params['phone'], $params['code'], $params['login_pwd'], $params['invitation_code'] ?? null);

        $this->success($result);
    }

    /**
     * 获取验证码
     *
     * @param CodeRequest $request
     */
    public function get_code(CodeRequest $request)
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);

        $code = substr((string)$this->generator->generate(), -6);
        $this->sms->sendCode($params['phone'], gConfig('ali_templateCode_login', ''), [
            'code' => $code
        ]);

        // TODO 开发环境将验证码报告给前端
        $this->success($code);
    }

    /**
     * 找回密码 - 验证手机
     *
     * @param retrievePhoneRequest $request
     */
    public function retrievePhone ( retrievePhoneRequest $request )
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);
        isset($params['code']) && $params['code'] = trim($params['code']);

        $this->service->retrievePhone($params['phone'], $params['code']);

        $this->success();
    }

    /**
     * 找回密码
     *
     * @param retrieveRequest $request
     */
    public function retrieve( retrieveRequest $request )
    {
        $params = $request->all();

        isset($params['phone']) && $params['phone'] = trim($params['phone']);
        isset($params['login_pwd']) && $params['login_pwd'] = trim($params['login_pwd']);

        $update = $this->service->dao->retrieve([
            'login_pwd' => password_hash($params['login_pwd'], PASSWORD_DEFAULT )
        ],[
            'phone' => $params['phone']
        ]);

        !$update && $this->error();

        $this->success();
    }

}